<?php

require_once( '../system/smarty.inc' );

require_once( '../system/tdao/Class_T_MON_CELL.php' );
require_once( '../system/tdao/Class_T_MON_CELL_WEEK.php' );;
require_once( '../system/tdao/Class_T_PATTERN.php' );

require_once( '../system/mdao/Class_M_OTHER.php' );

require_once( '../system/Class_ERROR.php' );
require_once( '../system/login.inc.php' );
require_once( '../system/aoki_m.php' );
require_once( '../system/aoki_m_plus.php' );
require_once( '../system/Class_PWD.php' );

Class_PWD::valid_auth(AOKI_AUTH_MON_CELL,$login['staff_auth']);

$dosearch = FALSE ;
$dao     = new Class_T_MON_CELL_WEEK;
$dao_mg  = new Class_T_MON_CELL;
$err     = new Class_ERROR;

if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ){
	$tmpfilename = "monthcell".date("Ymd_His");
	header("Content-Type: application/octet-stream"); 
	header("Content-Disposition: attachment; filename={$tmpfilename}.csv");
}


if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ||
	 isset( $_GET['hist'] ) &&  (  $_GET['hist'] == "2" ) ) {
	$forms =  $_SESSION['MSEARCH']['MON_CELL_WEEK'];
}else {
	$forms = $_GET;
	$_SESSION['MSEARCH']['MON_CELL_WEEK'] = $forms;
}

if ( isset( $forms['year4'] ) &&  ( !empty( $forms['year4'] ) ) ){
	$wherearr[] = sprintf("b.year4 =  '%s' ",  $dao->esc( $forms['year4'] ) );
}

if ( isset( $forms['week_cd'] ) &&  ( !empty( $forms['week_cd'] ) ) ){
	$matrix_cd = $forms['week_cd'];
}

if (isset($forms["sort"])) {
	if ($forms["sort"] == '2') {
		$aja_cd= $forms['areauser'];
	}
	
	if ($forms["sort"] == '3') {
		$zone_cd = $forms['zone_cd'];
		$zone_cd = trim($zone_cd);
		$aja_cd = $zone_cd;
	}
}

if ( $login['staff_auth'] == 'G-2' || $login['staff_auth'] == 'G-3' ) {
	$aja_cd   = $login['autoid'];
	$smarty->assign('isG23', $isG23 );
}
$no_error = true;
if  ( ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ) || ( isset( $forms['search'] ) && (  !empty( $forms['search'] ) ) ) ) {
	$dosearch = true;
	if ( empty($matrix_cd) ) {
		$err_mes['week_cd'] = '<p class="error">週名は入力必須です。</p>';
		$no_error = false;
	}
	
	if ( empty($aja_cd) ) {
		$err_mes['aja_cd'] = '<p class="error">AJAREKSコードは入力必須です。</p>';
		$no_error = false;
	}
}
$params = null;
if  ( !empty( $forms['sshop_cd']) ) {
	$params["shop_cd"] = $forms['sshop_cd'];
}
if  ( !empty( $forms['title']) ) {
	$params["title"]      = $forms['title'];
	$params["matrix_cd"]  = $matrix_cd;
}
if  ( !empty( $forms['upd_f']) ) {
	$params["upd_f"] = $forms['upd_f'];
	$params["matrix_cd"]  = $matrix_cd;
}
if  ( !empty( $forms['upd_t']) ) {
	$params["upd_t"] = $forms['upd_t'];
	$params["matrix_cd"]  = $matrix_cd;
}

/*
if ( isset( $forms['map_no'] ) &&  ( !empty( $forms['map_no'] ) ) ){
	$wherearr[] = sprintf(" a.map_no =  '%s' ", $dao->esc( $forms['map_no'] ) );
}



if ( isset( $forms['shop_cd'] ) &&  ( !empty( $forms['shop_cd'] ) ) ){
	$wherearr[] = sprintf(" a.shop_cd =  '%s' ", $dao->esc( $forms['shop_cd'] ) );
}


if ( isset( $forms['congruent_ins'] ) &&  ( !empty( $forms['congruent_ins'] ) ) ){
	$wherearr[] = sprintf(" a.congruent_ins like '%s' ",  "%".$dao->esc( $forms['congruent_ins'] )."%" );
}

if ( isset( $forms['mark'] ) &&  ( !empty( $forms['mark'] ) ) ){
	$wherearr[] = sprintf(" a.mark =  '%s' ",  $dao->esc( $forms['mark'] ) );
}



if ( isset( $forms['upd_f'] ) &&  ( !empty( $forms['upd_f'] ) ) ){
	$wherearr[] = Class_PWD::get_u_time_where_sql($forms['upd_f'], 1, "a" );
}

if ( isset( $forms['upd_t'] ) &&  ( !empty( $forms['upd_t'] ) ) ){
	$wherearr[] = Class_PWD::get_u_time_where_sql($forms['upd_t'],2, "a" );
}
*/
//ゾーンとエリアの計算開始
require_once( '../system/cell_ajareks_sort.inc.php' );
//ゾーンとエリアの計算終了
if ( isset($matrix_cd) ) {
	$data_title     = $dao_mg->getTitle( $matrix_cd );
}
require_once( '../system/cell_parametas.inc.php' );

	if ( $no_error && $dosearch ) {
		$is_final = true; 
	//-------------各パラメタ―作成-------------------------------------------------------------

	//-------------各パラメタ―作成完了---------------------------------------------------------
		$datas = $dao_mg->getMapGroupShop( $matrix_cd , $aja_cd, $params );
		//レギュラー   イレギュラー  地域1-5整形
		$shopmapno = array();
		foreach  ( $datas as $k => $v) {
			$shopmapno[$k] = array("r"=>$v["rgyu"], "i"=>$v["irgyu"],"l1"=>$v["lnews1"],"l2"=>$v["lnews2"],"l3"=>$v["lnews3"],"l4"=>$v["lnews4"],"l5"=>$v["lnews5"] );
			$datas[$k]["rgyu"] = "";
			$datas[$k]["irgyu"] = "";
			$datas[$k]["lnews1"] = "";
			$datas[$k]["lnews2"] = "";
			$datas[$k]["lnews3"] = "";
			$datas[$k]["lnews4"] = "";
			$datas[$k]["lnews5"] = "";
		}
		//print_r($datas);
		$details = $dao_mg->getCellDetails( $matrix_cd, $is_final, $aja_cd, $params );
		
		foreach ( $details as $k => $v) {
			$bSize   = strtolower( $v["size"] );
			$shop_cd = $v["shop_cd"];
			$wtype   = $v["wtype"];//9  非例外曜日　0-6 例外曜日の番号　
			$qtype   = $v["qtype"];//0  通常チラシ　1 地域１、2 地域2、3 地域3、4 地域4、5 地域5]
			if ( $qtype == "0" ) {
				if ( $wtype == "9" ) {
					$mytitle = "title_cd" ;
					$mypc    = "pc_".$bSize;
					$mynpc   = "npc_".$bSize;
					
					$datas[$shop_cd][$mytitle] = $v["title"];
					$datas[$shop_cd][$mypc]    = $data_pattern_arr[ $v["pattern_cd"] ];
					$datas[$shop_cd][$mynpc]   = $v["pnums"];
					if ( $maparr = $data_title_map_arr[ $v["title_cd"] ] ) {
						if ($maparr[0] == 2) {
							$datas[$shop_cd]["irgyu"]  = $shopmapno[$shop_cd]["i"];
							$datas[$shop_cd]["hirgyu"] =  $shopmapno[$shop_cd]["i"];
						} elseif ($maparr[0] == 3) {
							$datas[$shop_cd]["irgyu"]  = $maparr[1];
							$datas[$shop_cd]["hirgyu"] =  $shopmapno[$shop_cd]["i"];
						} else {
							$datas[$shop_cd]["rgyu"] = $shopmapno[$shop_cd]["r"];
						}
					}
				} else {
					$mytitle = $data_cell_wsets[$wtype]["title_cd"] ;
					$mypc    = $data_cell_wsets[$wtype]["pc_".$bSize];
					$mynpc   = $data_cell_wsets[$wtype]["npc_".$bSize];
					
					$datas[$shop_cd][$mytitle] = $v["title"];
					$datas[$shop_cd][$mypc]    = $data_pattern_arr[ $v["pattern_cd"] ];
					$datas[$shop_cd][$mynpc]   = $v["pnums"];
					
					if ( $maparr = $data_title_map_arr[ $v["title_cd"] ] ) {
						if ($maparr[0] == 2) {
							$datas[$shop_cd]["irgyu"]  = $shopmapno[$shop_cd]["i"];
							$datas[$shop_cd]["hirgyu"] =  $shopmapno[$shop_cd]["i"];
						} elseif ($maparr[0] == 3) {
							$datas[$shop_cd]["irgyu"]  = $maparr[1];
							$datas[$shop_cd]["hirgyu"] =  $shopmapno[$shop_cd]["i"];
						} else {
							$datas[$shop_cd]["rgyu"] = $shopmapno[$shop_cd]["r"];
						}
					}
				}
				
				
			}else {
				if ( $wtype == "9" ) {
					$mytitle = $data_cell_losets[$qtype]["title_cd"] ;
					$mypc    = $data_cell_losets[$qtype]["pc_".$bSize];
					$mynpc   = $data_cell_losets[$qtype]["npc_".$bSize];
					
					$datas[$shop_cd][$mytitle] = $v["title"];
					$datas[$shop_cd][$mypc]    = $data_pattern_arr[ $v["pattern_cd"] ];
					$datas[$shop_cd][$mynpc]   = $v["pnums"];
					$datas[$shop_cd][ "lnews".$qtype  ] =  $shopmapno[$shop_cd][ "l".$qtype ];
				} else {
					/*
					$dweeksets = $data_cell_losets[$qtype]["weeks"] ;
					
					$mytitle = $dweeksets[$wtype]["title_cd"] ;
					$mypc    = $dweeksets[$wtype]["pc_".$bSize];
					$mynpc   = $dweeksets[$wtype]["npc_".$bSize];
					
					$datas[$shop_cd][$mytitle] = $v["title_cd"];
					$datas[$shop_cd][$mypc]    = $v["pattern_cd"];
					$datas[$shop_cd][$mynpc]   = $v["pnums"];
					*/
				}
			}
		}
		//データ整形開始
		$data_mg     = $dao_mg->getMatrixTerm( $matrix_cd);

		//前年データを抽出する
		$pre_matrix_cd = $data_mg["pre_matrix_cd"];
		//usset($details);
		
		$details = $dao_mg->getCellDetails( $pre_matrix_cd, TRUE, $aja_cd,$params );
		foreach ( $details as $k => $v) {
			$bSize   = strtolower( $v["size"] );
			$shop_cd = $v["shop_cd"];
			$wtype   = $v["wtype"];//9  非例外曜日　0-6 例外曜日の番号　
			$qtype   = $v["qtype"];//0  通常チラシ　1 地域１、2 地域2、3 地域3、4 地域4、5 地域5]
			if ( $qtype == "0" && isset($datas[$shop_cd]) ) {
				if ( $wtype == "9" ) {
					$mytitle = "ltitle_cd" ;
					$mypc    = "lpc_".$bSize;
					$mynpc   = "lnpc_".$bSize;
					
					$datas[$shop_cd][$mytitle] = $v["title"];
					$datas[$shop_cd][$mypc]    = $data_pattern_arr[ $v["pattern_cd"] ];
					$datas[$shop_cd][$mynpc]   = $v["pnums"];
				} else {
					$mytitle = "l".$data_cell_wsets[$wtype]["title_cd"] ;
					$mypc    = "l".$data_cell_wsets[$wtype]["pc_".$bSize];
					$mynpc   = "l".$data_cell_wsets[$wtype]["npc_".$bSize];
					
					$datas[$shop_cd][$mytitle] = $v["title"];
					$datas[$shop_cd][$mypc]    = $data_pattern_arr[ $v["pattern_cd"] ];
					$datas[$shop_cd][$mynpc]   = $v["pnums"];
				}
			}else {
				if ( $wtype == "9" ) {
					$mytitle = "l".$data_cell_losets[$qtype]["title_cd"] ;
					$mypc    = "l".$data_cell_losets[$qtype]["pc_".$bSize];
					$mynpc   = "l".$data_cell_losets[$qtype]["npc_".$bSize];
					
					$datas[$shop_cd][$mytitle] = $v["title"];
					$datas[$shop_cd][$mypc]    = $data_pattern_arr[ $v["pattern_cd"] ];
					$datas[$shop_cd][$mynpc]   = $v["pnums"];
				} else {
					/*
					$dweeksets = $data_cell_losets[$qtype]["weeks"] ;
					
					$mytitle = $dweeksets[$wtype]["title_cd"] ;
					$mypc    = $dweeksets[$wtype]["pc_".$bSize];
					$mynpc   = $dweeksets[$wtype]["npc_".$bSize];
					
					$datas[$shop_cd][$mytitle] = $v["title_cd"];
					$datas[$shop_cd][$mypc]    = $v["pattern_cd"];
					$datas[$shop_cd][$mynpc]   = $v["pnums"];
					*/
				}
			}
		}
	//前年データを整形完了
		/*
		foreach ( $datas as $k => $v) {
			if (isset($v["title_cd"])) {
				$maintitleID = $v["title_cd"];
				if  ( isset($data_title_map_arr[$maintitleID]) ) {
					$datas[$k]["irgyu"] = $data_title_map_arr[$maintitleID];
				}
			}
		}
		*/
		//----------------------------------------------------------------------------
	//---CSVダウンロード　処理開始
		
	if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ){
		
		$_GET["csv"] = "";

		//1 string code 2 integer 3 date 4 string ""必要 
		$col_heads = array(
			'matrix_cd'    => array( 'マトリックスID',   1),
			'year4'        => array( '年度',             1),
			'week_seq'     => array( '週',               1),
			'flier_date'   => array( '折込日',           1),
			'status'       => array( '状態',             1),
			'mark'         => array( '合同',             1),
			'login_id'     => array( 'ゾーンAJAコード',  1),
			'user_name'    => array( 'ゾーンAJA名',      1),
			'rgyu'         => array( 'レギュラーMAP',    1),
			'irgyu'        => array( 'イレギュラーMAP',  1),
			'lnews1'       => array( '地域1MAP',      1),
			'lnews2'       => array( '地域2MAP',      1),
			'lnews3'       => array( '地域3MAP',      1),
			'lnews4'       => array( '地域4MAP',      1),
			'lnews5'       => array( '地域5MAP',      1),
			'year_sale'    => array( '年商',          1),
			'shop_cd'      => array( '店舗コード',    1),
			'shop_name'    => array( '店舗名',        1),
			'ly_sale6'     => array( '前年実績(土)',  1),
			'ly_sale7'     => array( '前年実績(日)',  1),
			'ly_sale'      => array( '前年実績(計)',  1),
	
			'ltitle_cd'    => array( '普通 前年セールタイトル',  1),
			'title_cd'     => array( '普通 本年セールタイトル',  1),
			'lnpc_b2'      => array( '普通 B2前年チラシ部数',  3),
			'lpc_b2'       => array( '普通 B2前年パターン',    1),
			'npc_b2'       => array( '普通 B2本年チラシ部数',  3),
			'pc_b2'        => array( '普通 B2本年パターン',    1),
			
			'lnpc_b3'      => array( '普通 B3前年チラシ部数',  3),
			'lpc_b3'       => array( '普通 B3前年パターン',    1),
			'npc_b3'       => array( '普通 B3本年チラシ部数',  3),
			'pc_b3'        => array( '普通 B3本年パターン',    1),
			
			'lnpc_b4'      => array( '普通 B4前年チラシ部数',  3),
			'lpc_b4'       => array( '普通 B4前年パターン',    1),
			'npc_b4'       => array( '普通 B4本年チラシ部数',  3),
			'pc_b4'        => array( '普通 B4本年パターン',    1)
		);
		//例外曜日
		foreach( $data_cell_wsets as $k => $v) {
			$name =$v["name"];
			$col_heads[ $v["title_cd"] ] = array( "{$name} 本年セールタイトル",    1);
			
			$col_heads[ $v["npc_b2"] ]   = array( "{$name} B2本年チラシ部数",   3);
			$col_heads[ $v["pc_b2"] ]    = array( "{$name} B2本年パターン",     1);
			
			$col_heads[ $v["npc_b3"] ]   = array( "{$name} B3本年チラシ部数",   3);
			$col_heads[ $v["pc_b3"] ]    = array( "{$name} B3本年パターン",     1);
			
			$col_heads[ $v["npc_b4"] ]   = array( "{$name} B4本年チラシ部数",   3);
			$col_heads[ $v["pc_b4"] ]    = array( "{$name} B4本年パターン",     1);
		}
		//地方
		foreach( $data_cell_losets as $k => $v) {
			$name =$v["name"];
			$col_heads[ $v["ltitle_cd"] ]= array( "{$name} 前年セールタイトル",    1);
			$col_heads[ $v["title_cd"] ] = array( "{$name} 本年セールタイトル",    1);
			
			$col_heads[ $v["lnpc_b2"] ]  = array( "{$name} B2前年チラシ部数",   3);
			$col_heads[ $v["lpc_b2"] ]   = array( "{$name} B2前年パターン",     1);
			$col_heads[ $v["npc_b2"] ]   = array( "{$name} B2本年チラシ部数",   3);
			$col_heads[ $v["pc_b2"] ]    = array( "{$name} B2本年パターン",     1);
			
			$col_heads[ $v["lnpc_b3"] ]  = array( "{$name} B3前年チラシ部数",   3);
			$col_heads[ $v["lpc_b3"] ]   = array( "{$name} B3前年パターン",     1);
			$col_heads[ $v["npc_b3"] ]   = array( "{$name} B3本年チラシ部数",   3);
			$col_heads[ $v["pc_b3"] ]    = array( "{$name} B3本年パターン",     1);
			
			$col_heads[ $v["lnpc_b4"] ]  = array( "{$name} B4前年チラシ部数",   3);
			$col_heads[ $v["lpc_b4"] ]   = array( "{$name} B4前年パターン",     1);
			$col_heads[ $v["npc_b4"] ]   = array( "{$name} B4本年チラシ部数",   3);
			$col_heads[ $v["pc_b4"] ]    = array( "{$name} B4本年パターン",     1);
		}
		
		
		$col_heads["u_time"] = array( '更新日時',          3);
		$col_heads["c_time"] = array( '登録日時',          3);
		$col_heads["m_user_name"] = array( '更新者',       1);
	
		//列名
		foreach ($col_heads as $k=>$v) {
			$colums["name"][]   = $k;
			$colums["head"][]   = $v[0];
			$colums["type"][$k] = $v[1];
		}

		$statusID    = Class_PWD::getMatrixStatues( $data_mg );
		$dSaveTime   = $dao_mg->getMonCellSaveTimeMsg( $matrix_cd, $aja_cd);
		foreach ($datas as $k => $v) {
			$datas[$k]["matrix_cd"]   =  $matrix_cd;
			$datas[$k]["year4"]       =  $data_mg["year4"];
			$datas[$k]["week_seq"]    =  $data_mg["week_seq"];
			$datas[$k]["flier_date"]  =  Class_PWD::int8_to_dateD( $data_mg["flier_date"] );;
			$datas[$k]["status"]      =  $aoki_matrix_status[$statusID];
			if (isset($dSaveTime[$k])) {
				$datas[$k]["c_time"]      =  Class_PWD::dateTime_to_YmdHi($dSaveTime[$k]["c_time"]);
				$datas[$k]["u_time"]      =  Class_PWD::dateTime_to_YmdHi($dSaveTime[$k]["u_time"]);
				$datas[$k]["m_user_name"] =  $dSaveTime[$k]["m_user_name"];
			}
		}
		
		//データ整形完了
		$dao->output_csv($datas, $colums);
		exit;
	}
	//---CSVダウンロード　処理完了
	

	/*
	if( isset( $forms['page'] ) && is_numeric( $forms['page'] ) ){
		$dosearch = TRUE;
		$page['current'] = $forms['page'];	
	}else{
		$page['current'] = 1;
	}
	$page['item'] = PAGE_MAX;
	*/
	if ($dosearch) {
		//表示ために整形開始
	$count = count($datas);
		
		//表示ために整形完了
			
		
	}
}
if (empty($forms["year4"])) {
	$forms["year4"] = date('Y');
}

if( isset($forms) )      $smarty->assign('forms', $forms);
if( isset($data) )       $smarty->assign('data', $data);
if( isset($edit_msg) )   $smarty->assign('edit_msg', $edit_msg );
	$smarty->assign('title',      $data_title_arr );
	$smarty->assign('title_map',  $data_title_map_arr );
	$smarty->assign('title_week', $data_title_week_arr );
	
	//$smarty->assign('pattern',      $data_pattern_arr );
	//$smarty->assign('pattern_nums', $data_pattern_nums );
	$smarty->assign('cell_wsets',   $data_cell_wsets );
	$smarty->assign('cell_losets',  $data_cell_losets );
if( isset($page) )        $smarty->assign('page', $page);
if( isset($err_mes ) )    $smarty->assign('err', $err_mes);
if( isset($sqlmessage ) ) $smarty->assign('errcopy', $sqlmessage);
if( isset($sqldelmsg ) )  $smarty->assign('errdel', $sqldelmsg);
if( isset($datas) )       $smarty->assign('data',   $datas);
if( isset($count) )       $smarty->assign('count',  $count);
$smarty->assign('year4',  Class_PWD::get_years());

$week= Class_M_OTHER::get_week_array( $forms["year4"], PLEASE_SELECT_ARR );
$smarty->assign('week', $week );

$map_sort[""] = "すべて";
foreach ($aoki_map_sort as $k => $v) {
	$map_sort[$k] = $v;
}
$smarty->assign('map_sort', $map_sort );

$map_mark[""] = "すべて";
foreach ($aoki_congruent as $k => $v) {
	$map_mark[$k] = $v;
}
$smarty->assign('mark',     $map_mark );

$smarty->assign('menu',  $smarty->fetch('menu.html') );
$smarty->assign('logout', $smarty->fetch('logout.html') );
$smarty->assign('footer', $smarty->fetch('footer.html') );
$smarty->assign('pageing', $smarty->fetch('page.html') );
$smarty->display('tmoncell_index.html');

?>